<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible">
    <el-form ref="modalForm" :model="dataSource" status-icon>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item
            v-if="modalType === 'edit'"
            label="工资信息编号"
            :label-width="formLabelWidth"
          >
            <el-input v-model="dataSource.salaryCode" disabled autocomplete="off" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="绑定员工："
            prop="empCode"
            :label-width="formLabelWidth"
            :rules="[
              { required: true, message: '请绑定员工',trigger:'blur,change'}
            ]"
          >
            <el-select v-model="dataSource.empCode" :value="dataSource.empCode" placeholder="请绑定员工">
              <el-option
                v-for="item in empArrayList"
                :key="item.empCode"
                :label="item.userInfoName"
                :value="item.empCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="15">
          <el-form-item
            label="基本工资："
            :label-width="formLabelWidth"
            prop="basicSalary"
            :rules="[
              { required: true, message: '基本工资不能为空',trigger:'blur,change'}
            ]"
          >
            <el-input-number v-model="dataSource.basicSalary" placeholder="请输入基本工资额度" :value="dataSource.basicSalary" style="width: 300px" :precision="2" :step="1000.00" :min="0.0" :max="50000" @change="changeSalary" />
            <span style="margin-left: 10px"> 元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="15">
          <el-form-item
            label="综合保险费用："
            :label-width="formLabelWidth"
            prop="allInsurance"
            :rules="[
              { required: true, message: '综合保险费用不能为空',trigger:'blur,change'}
            ]"
          >
            <el-input-number v-model="dataSource.allInsurance" placeholder="请输入综合保险额度" style="width: 300px" :precision="2" :step="100.00" :max="2000" :min="0.0" @change="changeSalary" />
            <span style="margin-left: 10px"> 元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="15">
          <el-form-item
            label="奖金："
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="dataSource.awardSalary" placeholder="请输入奖金额度" style="width: 300px;" :precision="2" :step="100.00" :max="2000" :min="0.0" @change="changeSalary" />
            <span style="margin-left: 10px"> 元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="15">
          <el-form-item
            label="罚金："
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="dataSource.finedSalary" placeholder="请输入罚金额度" style="width: 300px" :precision="2" :step="20.00" :min="0.0" :max="500" @change="changeSalary" />
            <span style="margin-left: 10px"> 元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="15">
          <el-form-item
            label="实际工资："
            :label-width="formLabelWidth"
          >
            <el-input-number v-model="dataSource.finalSalary" disabled style="width: 300px" :precision="2" />
            <span style="margin-left: 10px"> 元</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item
        label="惩罚原因："
        :label-width="formLabelWidth"
        prop="finedResion"
        :rules="[
          { min: 3,max:100, message: '长度在3-100个字符之间',trigger:'change'}
        ]"
      >
        <el-input v-model="dataSource.finedResion" type="textarea" autosize autocomplete="off" />
      </el-form-item>
      <el-alert
        title="温馨提示："
        type="info"
        :closable="false"
      />
      <el-alert
        title="1. 为了确定实际的工资额度，请您点击确认每个额度的大小值。"
        type="info"
        :closable="false"
      />
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleConfirm('modalForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOneSalaryInfo, modifyOneSalaryInfo } from '@/api/ht/yytgl/gzgl/gzxxcx'
const { Message } = require('element-ui')
const { getAllEmpInfoList } = require('@/api/ht/yytgl/yggl/ygxxgl')
export default {
  name: 'Modal',
  props: {
    salaryInfo: {
      type: Object,
      default: () => {
        return {
          salaryCode: '',
          id: '',
          empCode: '',
          basicSalary: '',
          allInsurance: '',
          awardSalary: '',
          finedSalary: '',
          finalSalary: '',
          finedResion: ''
        }
      }
    },
    departmentInfoList: {
      type: Array,
      default: () => []
    },
    employeeTypelist: {
      type: Array,
      default: () => []
    },
    vacationTypeList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      title: '员工工资信息详情',
      formLabelWidth: '150px', // 表单输入框字段宽度
      dialogFormVisible: false, // 对话框是否显示
      empArrayList: [], // 员工数组列表
      modalType: 'add',
      dataSource: { // 数据源
      }
    }
  },
  watch: {
    salaryInfo(newdata, prevdata) {
      this.dataSource = newdata
      this.title = '工资信息详情' + this.dataSource.id
    }
  },
  methods: {
    checkModelType(type) {
      this.modalType = type
      // 展示对话框
      this.dialogFormVisible = true
      // 判断是编辑还是新增
      if (type === 'add') {
        // 置空
        this.title = '新增员工工资信息'
        this.dataSource = {}
      }
      this.getAllEmpInfoListHandle()
    },
    changeSalary() {
      this.dataSource.finalSalary = this.dataSource.basicSalary - this.dataSource.allInsurance + this.dataSource.awardSalary - this.dataSource.finedSalary
      if (this.dataSource.finalSalary < 0) {
        this.$message({
          message: '警告，该设置将导致’最终工资‘为’零‘，请检查！',
          type: 'warning'
        })
        this.dataSource.finalSalary = 0
      }
    },
    handleConfirm(formName) {
      const type = this.modalType
      // 判断是编辑还是新增
      if (type === 'add') {
        // 调用新增请求
        this.addSalaryInfoHandle(formName)
      } else {
        // 调用修改请求
        this.modifySalaryInfoHandle(formName)
      }
    },
    getAllEmpInfoListHandle() {
      this.getAllEmpInfo()
    },
    addSalaryInfoHandle(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addSalaryInfo()
        } else {
          console.log('表单缺少数据或者不符合要求')
          return false
        }
      })
    },
    modifySalaryInfoHandle(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.modifySalaryInfo()
        } else {
          console.log('表单缺少数据或者不符合要求')
          return false
        }
      })
    },
    async getAllEmpInfo() {
      const res = await getAllEmpInfoList()
      const data = res.data.main
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        this.empArrayList = data
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    async addSalaryInfo() {
      const res = await addOneSalaryInfo(this.dataSource)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'success',
          duration: 2 * 1000
        })
        this.dialogFormVisible = false
        this.reflushDataHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    async modifySalaryInfo() {
      const res = await modifyOneSalaryInfo(this.dataSource)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'success',
          duration: 2 * 1000
        })
        this.dialogFormVisible = false
        this.reflushDataHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    reflushDataHandle() {
      // 调用父组件方法刷新数据
      this.$emit('reflushDataHandle')
    }
  }
}
</script>

<style scoped>

</style>
